<?php

/**
 * This file is part of the Anota project.
 * 2011-2012 Fernando Garcia Huerta <fgarciahue@uoc.edu>
 * 
 * UOC - FUNDAMENTOS DE INFORMÁTICA EN ENTORNOS BIOINFORMÁTICOS - PEC2
 */

/**
 * Module Base
 * 
 * Base class for all modules
 *  - manages common template and reder behaviours.
 * 
 * @package    Anota
 * @subpackage application/module
 * @author     Fernando Garcia Huerta <fgarciahue@uoc.edu>
 */

class AnotaModuleBase {

    protected
    $config = null,         // config of the application
    $template = null,       // default template of the module
    $template_file = null;  // deafult template file name

    /**
     * Constructor.
     * 
     * @param AnotaConfig $config configuration object
     */
    public function __construct($config) {
        $this->config = $config;
    }

    /**
     * Sets template for the module
     * 
     * @param string $template template name
     */
    public function setTemplate($template) {
        $this->template = $template;
        $this->template_file = $this->config->getRootDir() . DIRECTORY_SEPARATOR . $template;
    }

    /**
     * Renders the template and returns the resulted content
     * 
     * @return string
     */
    public function render() {        
        if (null !== $this->template && file_exists($this->template_file)) {
            ob_start();
            include ($this->template);
            $content = ob_get_contents();
            ob_end_clean();
        }

        return $content;
    }

}
